<div id="content" class="span12">
    <div class="row">
        <div id="actions-toolbar" class="span8">
            <h1>Dashboard</h1>
            <div class="btn-toolbar">
                <div class="btn-group">
                    <?php echo CHtml::link('<i class="icon-leaf icon-white"></i> Crop Entry', array('app/newcrop'), array('class' => 'btn btn-info')); ?>
                    <?php echo CHtml::link('<i class="icon-barcode icon-white"></i> Price Entry', array('app/newprice'), array('class' => 'btn btn-info')); ?>
                    <?php //echo CHtml::link('<i class="icon-share-alt icon-white"></i> Send', '#dialog', array('class' => 'btn btn-primary', 'data-toggle' => 'modal', 'onClick' => 'triggerFrontlineSMS();')); ?>
                </div>
            </div>
        </div>
        <div class="span12">
            <?php
            $this->widget('zii.widgets.grid.CGridView', array(
                'id' => 'data-grid',
                'dataProvider' => $model->search(),
                //'filter' => $model,
                'columns' => array(
                    array(
                        'name' => 'variety',
                        'header' => 'Crop Variety',
                    ),
                    array(
                        'name' => 'type',
                        'header' => 'Crop Type',
                        'value' => ucfirst('$data->type'),
                    ),
                    array(
                        'header' => 'Last Price(in Peso)',
                        'value' => '($amount = $data->getLastPrice($data->id)) ? $amount : "n/a"',
                    ),
                    array(
                        'header' => 'Last Updated On',
                        'value' => '($amount = $data->getLastPriceUpdate($data->id)) ? Yii::app()->dateFormatter->format("h:mm a - MMM d, yyyy (EEEE)", $amount) : "n/a"',
                    ),
                    array(
                        'name' => 'is_active',
                        'header' => 'Status',
                        'value' => '($data->is_active == 1) ? "Active" : "Inactive"',
                    ),
                    array(
                        'class' => 'ActionButtonColumn',
                        'buttons' => array(
                            'pricehistory' => array(
                                'label' => '\'<i class="icon-time"></i> Price History\'',
                                'url' => 'Yii::app()->controller->createUrl("app/pricehistory",array("cid"=>$data->id))',
                                'options' => array(
                                    'class' => '\'btn btn-small\'',
                                    'title' => 'View price history',
                                ),
                            ),
                            'setactive' => array(
                                'label' => '($data->is_active == 1) ? \'<i class="icon-ban-circle icon-white"></i> Deactivate\' : \'<i class="icon-ok-circle icon-white"></i> Activate\'',
                                'url' => 'Yii::app()->controller->createUrl("cropstatus",array("cid"=>$data->id))',
                                'options' => array(
                                    'class' => '($data->is_active == 1) ? \'cropstat btn btn-small btn-danger\' : \'cropstat btn btn-small btn-success\'',
                                    'data-status' => '($data->is_active == 1) ? \'active\' : \'inactive\'',
                                    'title' => 'Change status',
                                ),
                            )
                        ),
                        'template' => '{pricehistory} {setactive}',
                    ),
                ),
                'cssFile' => false,
                'itemsCssClass' => 'table',
                'summaryCssClass' => 'pull-right',
            ));
            ?>
        </div>
    </div>
</div>


<?php
Yii::app()->clientScript->registerScript('cropstatus', "
    jQuery('#data-grid a.cropstat').live('click',function() {
        var status = ($(this).attr('data-status') == 'active') ? 'inactive' : 'active';
        
        if(!confirm('Are you sure you want to mark this crop as ' + status + '?')) return false;
        
        var url = $(this).attr('href');

        $.post(url,function(res){
             window.location.reload();
         });
        return false;
    });
");
?>

<script>
    function triggerFrontlineSMS() {
        <?php
        echo CHtml::ajax(array(
            'url' => array('app/httptrigger'),
            'type' => 'post',
            'dataType' => 'json',
            'success' => 'function(data) {
                $("#dialog").html(data.message);
            }'
        ));
        ?>
        return false;
    }
</script>